﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
using System.Reflection;
using Portal.Workflow.Model;
using Portal.Core;
using Portal.Workflow.Render.Template;

namespace Portal.Workflow.Utilities
{
    public class WorkflowHomeRenderMan
    {
        public static IHomeWorkflowRender[] LoadAssembly(string filename)
        {
            ArrayList ArrayListModules = new ArrayList();
            Assembly a = Assembly.LoadFrom(filename);
            string Msg = "Assembly not found or not valid";
            if (a == null) throw new Exception(Msg);
            foreach (Module mod in a.GetLoadedModules())
            {
                foreach (Type ty in mod.GetTypes())
                {
                    foreach (Type intf in ty.GetInterfaces())
                    {
                        if (intf == typeof(IHomeWorkflowRender))
                        {
                            try
                            {
                                IHomeWorkflowRender w = (IHomeWorkflowRender)a.CreateInstance(ty.FullName);
                                if (!string.IsNullOrEmpty(w.ServiceId))
                                {
                                    ArrayListModules.Add(w);
                                }
                            }
                            catch (System.Exception ex)
                            {
                                ex.Source = "";
                            }
                        }
                    }
                }
            }
            IHomeWorkflowRender[] ws = new IHomeWorkflowRender[ArrayListModules.Count];
            for (int iIndex = 0; iIndex < ArrayListModules.Count; iIndex++)
            {
                ws[iIndex] = (IHomeWorkflowRender)ArrayListModules[iIndex];
            }
            return ws;
        }

        public static IHomeWorkflowRender Find(string DllPath, string ServiceId)
        {
            IHomeWorkflowRender[] ItemLoadWorkflowHomeRenderMan = LoadWorkflowHomeRenderMan(DllPath);
            for (int iIndex = 0; iIndex < ItemLoadWorkflowHomeRenderMan.Length; iIndex++)
            {
                if (ItemLoadWorkflowHomeRenderMan[iIndex].ServiceId.Equals(ServiceId))
                {
                    return ItemLoadWorkflowHomeRenderMan[iIndex];
                }
            }
            return null;
        }

        public static IHomeWorkflowRender[] LoadWorkflowHomeRenderMan(string DllPath)
        {
            string[] Files = System.IO.Directory.GetFiles(DllPath, "*.dll");
            ArrayList ArrayListServives = new ArrayList();
            for (int iIndex = 0; iIndex < Files.Length; iIndex++)
            {
                try
                {
                    string FileDll = Files[iIndex];
                    IHomeWorkflowRender[]
                        asms = LoadAssembly(FileDll);
                    for (int jIndex = 0; jIndex < asms.Length; jIndex++)
                    {
                        if (asms[jIndex].ServiceId != null)
                        {
                            ArrayListServives.Add(asms[jIndex]);
                        }
                    }
                }
                catch (System.Exception ex)
                {
                    ex.Source = "";
                }
            }
            IHomeWorkflowRender[] WorkflowHomeRenderMan = new IHomeWorkflowRender[ArrayListServives.Count];
            for (int iIndex = 0; iIndex < ArrayListServives.Count; iIndex++)
            {
                WorkflowHomeRenderMan[iIndex] = (IHomeWorkflowRender)ArrayListServives[iIndex];
            }
            return WorkflowHomeRenderMan;
        }
    }
}
